package com.wl156.activities;

import java.io.IOException;
import java.security.acl.Owner;

import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddCar extends Activity implements OnClickListener {

	EditText no;
	EditText owner;
	EditText ownerTel;
	EditText driver;
	EditText driverTel;
	EditText type;
	EditText demand;
	EditText carLength;
	EditText load;
	EditText tonnage;

	Button add;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_add_car);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		no = (EditText) findViewById(R.id.no);
		owner = (EditText) findViewById(R.id.owner);
		ownerTel = (EditText) findViewById(R.id.ownerTel);
		driver = (EditText) findViewById(R.id.driver);
		driverTel = (EditText) findViewById(R.id.driverTel);
		type = (EditText) findViewById(R.id.type);
		demand = (EditText) findViewById(R.id.demand);
		carLength = (EditText) findViewById(R.id.carLength);
		load = (EditText) findViewById(R.id.load);
		tonnage = (EditText) findViewById(R.id.tonnage);

		add = (Button) findViewById(R.id.add);
		add.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add:
			final String No = no.getText().toString();
			final String Owner = owner.getText().toString();
			final String OwnerTel = ownerTel.getText().toString();
			final String Driver = driver.getText().toString();
			final String DriverTel = driverTel.getText().toString();
			final String Type = type.getText().toString();
			final String Demand = demand.getText().toString();
			final String CarLength = carLength.getText().toString();
			final String Load = load.getText().toString();
			final String Tonnage = tonnage.getText().toString();

			if (TextUtils.isEmpty(No) || TextUtils.isEmpty(Owner)
					|| TextUtils.isEmpty(OwnerTel) || TextUtils.isEmpty(Driver)
					|| TextUtils.isEmpty(DriverTel) || TextUtils.isEmpty(Type)
					|| TextUtils.isEmpty(Demand)
					|| TextUtils.isEmpty(CarLength) || TextUtils.isEmpty(Load)
					|| TextUtils.isEmpty(Tonnage)) {
				Toast.makeText(AddCar.this, "请填写相应的字段", Toast.LENGTH_LONG)
						.show();
				return;
			}

			new AsyncTask<Void, Void, Object>() {

				@Override
				protected void onPostExecute(Object result) {
					if (result instanceof Exception) {
						Exception e = (Exception) result;

						Toast.makeText(AddCar.this, "添加失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(AddCar.this, "添加成功", Toast.LENGTH_LONG)
								.show();
						finish();
					}
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient.common_AddCarInfo(
								mCertificate, No, Owner, OwnerTel, Driver,
								DriverTel, Type, Demand,
								Float.valueOf(CarLength), Float.valueOf(Load),
								Float.valueOf(Tonnage));
					} catch (NumberFormatException e) {
						return e;
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();

			break;
		default:
			break;
		}
	}
}
